package org.exaple.laixe;

//import java.io.File;
//import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
//import java.net.URL;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Toast;
//import android.widget.Toast;

public class Laixe extends Activity implements OnClickListener {
    
	/**
	private Button btn_Next;
	private Button btn_Back;
	private WebView webView;
	private CheckBox chkCorrect;
	private Button btn_Question;
	private boolean showAnswer = false;
	private String currentQuestion = "230".toString();
	**/
	
	private Button btn_Hoc;
	private Button btn_Thi;
	private Button btn_Thoat;
	private Button btn_ThietLap;
	private Button btn_About;
	private Button btn_Sai;
	public static String hang = "B1";	// B1, B2, ....
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        btn_Hoc = (Button) findViewById(R.id.btn_Hoc);        
        btn_Hoc.setOnClickListener( this );
        btn_Thi = (Button) findViewById(R.id.btn_Thi);
        btn_Thi.setOnClickListener(this);
        btn_Thoat = (Button) findViewById(R.id.btn_Thoat);
        btn_Thoat.setOnClickListener(this);
        btn_ThietLap = (Button) findViewById(R.id.btn_ThietLap);
        btn_ThietLap.setOnClickListener(this);
        btn_About = (Button) findViewById(R.id.btn_About);
        btn_About.setOnClickListener(this);
        btn_Sai = (Button) findViewById(R.id.btn_Sai);
        btn_Sai.setOnClickListener(this);
        
    }
        
    public void onClick( View view ) {    	
    	switch ( view.getId() ) {
		case R.id.btn_Hoc:
			Intent i = new Intent( this, Hoc.class);
			startActivity( i );
			break;
		case R.id.btn_Thi:
			// create ALert Dialog to choose Hang(B1,B2, ...)			
			final Intent thi = new Intent(this, Thi.class );
			final CharSequence[] items = {"B1", "B2,C,D,E"};
			AlertDialog.Builder chooseHang = new AlertDialog.Builder(this);
			chooseHang.setTitle( "Chọn một hạng để thi" );
			chooseHang.setCancelable( false );
			chooseHang.setPositiveButton( "Đồng ý", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int id) {
					startActivity(thi);
				}
			});
			chooseHang.setNegativeButton( "Bỏ qua", new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int id) {
					dialog.cancel();	
				}
			});
			
			chooseHang.setSingleChoiceItems(items, 0, new DialogInterface.OnClickListener() {
				
				public void onClick(DialogInterface dialog, int item) {
					hang = items[item].toString();
				}
			});
			AlertDialog alert = chooseHang.create();	
			alert.show();
			break;		
		case R.id.btn_ThietLap:
			startActivity(new Intent(this, Prefs.class));
			break;
		case R.id.btn_About:
			startActivity(new Intent(this, About.class));
			break;
		case R.id.btn_Sai:
			startActivity(new Intent(this, Incorrect30Questions.class));
			break;
		case R.id.btn_Thoat:
			finish();
			break;
		
		default:
			break;
		}
    }
}
       
/**
        
        // find controls on the screen
        btn_Back = (Button) findViewById(R.id.btn_Back);
        btn_Next = (Button) findViewById(R.id.btn_Next);
        webView = (WebView) findViewById(R.id.web_view);
        chkCorrect = (CheckBox) findViewById(R.id.chk_Correct);
        btn_Question = (Button) findViewById(R.id.btn_Question);
        
        //turn on javascript 
        webView.getSettings().setJavaScriptEnabled(true);
        //
        // webView.loadUrl("file:///android_asset/234.html");
        //String alo = getStringFromURL("001.html");
        //Toast.makeText(this, alo,
        //		Toast.LENGTH_LONG).show();
        //String ola = "file:///android_asset/hpdi/";
        //String alo = getStringFromURL("237.html");
        //webView.loadDataWithBaseURL(ola, alo, "text/html", "utf-8", "");
        //
        
        ShowQuestion( currentQuestion );
        
        btn_Next.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				int currentQues = Integer.parseInt(currentQuestion);
				currentQues +=1;
				String current = "";
				if( currentQues <= 0 )
					currentQues = 1;
				if( currentQues >= 300)
					currentQues = 300;
				
				if( currentQues >= 1 && currentQues <= 9)
					current = "0" + "0" + String.valueOf( currentQues );
				else if( currentQues >=10 && currentQues <=99 )
					current  = "0" + String.valueOf( currentQues );
				else 
					current = String.valueOf( currentQues );
				
				currentQuestion = current;
				ShowQuestion( currentQuestion );
			}
		});
        
        btn_Back.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				int currentQues = Integer.parseInt(currentQuestion);
				currentQues -=1;
				String current = "";
				if( currentQues <= 0 )
					currentQues = 1;
				if( currentQues >= 300)
					currentQues = 300;
				
				if( currentQues >= 1 && currentQues <= 9)
					current = "0" + "0" + String.valueOf( currentQues );
				else if( currentQues >=10 && currentQues <=99 )
					current  = "0" + String.valueOf( currentQues );
				else 
					current = String.valueOf( currentQues );
				
				currentQuestion = current;
				ShowQuestion( currentQuestion );
			}
		});
        
        chkCorrect.setOnClickListener( new View.OnClickListener() {
			
			public void onClick(View v) {
				if( ((CheckBox)v).isChecked() ) {
					showAnswer = true;
				}
				else {
					showAnswer = false;
				}
				// Show Question with answer again
				ShowQuestion( currentQuestion );
			}
		});
    }
    
    public void ShowQuestion(String number) {
    	String baseUrl = "file:///sdcard/data/org.exaple.laixe/";
    	String data = getStringFromURL( number + ".html");
    	btn_Question.setText( currentQuestion );
    	webView.loadDataWithBaseURL(baseUrl, data, "text/html", "utf-8", "");
    }
    
    // Read File from Assets and return to a string to process later
    public String getStringFromURL(String s) {
    	String str = "";
    	try {
	    	InputStream is = getApplicationContext().getAssets().open(s);
	    	InputStreamReader input = new InputStreamReader(is);
	    	int charRead;	    	
	    	char[] bufer = new char[2000];
	    	while( (charRead = input.read(bufer)) > 0 ) {
	    		String readString = String.copyValueOf(bufer, 0, charRead);
	    		str += readString;
	    		bufer = new char[2000];
	    	}
	    	is.close();
    	}
    	catch(IOException io) {
    		io.printStackTrace();
    	}
    	// Process str when displaying anwser
    	if(showAnswer == true) {
    		// Đổi style:"display= none" -> true
    	    str = str.replaceAll( "display: none", "display: true");    	
    	}
    	return str;
    }
   **/
    
    